home *** CD-ROM | disk | FTP | other *** search
- { client.pas -- Simple (cold link) DDE client }
-
- program Client;
-
- {$R client.res}
-
- uses WinTypes, WinProcs, WObjects, Strings;
-
- const
-
- id_Menu = 100; { Menu resource ID }
- cm_Link = 101; { Link to server command ID }
- cm_Unlink = 102; { Unlink from server command ID }
- cm_GetData = 103; { Get data command ID }
- cm_Quit = 104; { Exit command ID }
- id_Data = 1; { Static text control ID }
-
- defaultText = '(No text received)'; { Default data string }
- msgCaption = 'Client Message Box'; { Message box caption }
- wndCaption = 'Client'; { Window caption }
- linkedCaption = 'Client-->Linked'; { Caption when linked }
-
- serverName = 'Server'; { Name of server application }
- serverTopic = 'Data'; { Name of data to be transferred }
-
- type
-
- ClientApplication = object(TApplication)
- procedure InitMainWindow; virtual;
- end;
-
- PConversation = ^TConversation;
- TConversation = object(TStatic)
- HWndServer: HWnd; { Server's window handle }
- LinkEstablished: Boolean; { True if conversing with server }
- constructor Init(AParent: PWindowsObject; AnId: Integer;
- ATitle: PChar; X, Y, W, H: Integer; ATextLen: Word);
- procedure WMDDEAck(var Msg: TMessage);
- virtual wm_First + wm_DDE_Ack;
- procedure WMDDEData(var Msg: TMessage);
- virtual wm_First + wm_DDE_Data;
- procedure WMDDETerminate(var Msg: TMessage);
- virtual wm_First + wm_DDE_Terminate;
- procedure LinkToServer;
- procedure UnlinkFromServer;
- procedure RequestText;
- end;
-
- PClientWindow = ^ClientWindow;
- ClientWindow = object(TWindow)
- DDEWindow: PConversation;
- constructor Init(AParent: PWindowsObject; ATitle: PChar);
- procedure WMInitMenu(var Msg: TMessage);
- virtual wm_First + wm_InitMenu;
- procedure CMLink(var Msg: TMessage);
- virtual cm_First + cm_Link;
- procedure CMUnlink(var Msg: TMessage);
- virtual cm_First + cm_Unlink;
- procedure CMGetData(var Msg: TMessage);
- virtual cm_First + cm_GetData;
- procedure CMQuit(var Msg: TMessage);
- virtual cm_First + cm_Quit;
- end;
-
-
- { ClientApplication }
-
- {- Initialize ClientApplication object's window }
- procedure ClientApplication.InitMainWindow;
- begin
- MainWindow := New(PClientWindow, Init(nil, wndCaption))
- end;
-
-
- { ClientWindow }
-
- {- Construct ClientWindow object }
- constructor ClientWindow.Init(AParent: PWindowsObject; ATitle: PChar);
- begin
- TWindow.Init(AParent, ATitle);
- with Attr do
- begin
- Menu := LoadMenu(HInstance, PChar(id_Menu));
- X := 10; Y := 10; W := 275; H := 300
- end;
- DDEWindow := New(PConversation, Init(@Self, id_Data, defaultText,
- 10, 100, 200, 35, StrLen(defaultText)))
- end;
-
- {- A menu is about to open. Gray any illegal commands. }
- procedure ClientWindow.WMInitMenu(var Msg: TMessage);
- begin
- with Attr do
- if Msg.WParam = Menu then
- begin
- if DDEWindow^.LinkEstablished then
- begin
- EnableMenuItem(Menu, cm_Link, mf_ByCommand or mf_Grayed);
- EnableMenuItem(Menu, cm_Unlink, mf_ByCommand or mf_Enabled);
- EnableMenuItem(Menu, cm_GetData, mf_ByCommand or mf_Enabled)
- end else
- begin
- EnableMenuItem(Menu, cm_Link, mf_ByCommand or mf_Enabled);
- EnableMenuItem(Menu, cm_Unlink, mf_ByCommand or mf_Grayed);
- EnableMenuItem(Menu, cm_GetData, mf_ByCommand or mf_Grayed)
- end
- end
- end;
-
- {- Establish a "conversation" with server }
- procedure ClientWindow.CMLink(var Msg: TMessage);
- begin
- DDEWindow^.LinkToServer
- end;
-
- {- Terminate the "conversation" with server }
- procedure ClientWindow.CMUnlink(var Msg: TMessage);
- begin
- DDEWindow^.UnlinkFromServer
- end;
-
- {- Request data from server }
- procedure ClientWindow.CMGetData(var Msg: TMessage);
- begin
- DDEWindow^.RequestText
- end;
-
- {- Exit application }
- procedure ClientWindow.CMQuit(var Msg: TMessage);
- begin
- CloseWindow
- end;
-
-
- { TConversation }
-
- {- Construct unlinked TConversation control window }
- constructor TConversation.Init(AParent: PWindowsObject; AnId: Integer;
- ATitle: PChar; X, Y, W, H: Integer; ATextLen: Word);
- begin
- TStatic.Init(AParent, AnId, ATitle, X, Y, W, H, ATextLen);
- LinkEstablished := false
- end;
-
- {- Respond to acknowledgement message from server }
- procedure TConversation.WMDDEAck(var Msg: TMessage);
- var
- AppAtom, AppTopic: TAtom;
- DDEStatus: Word;
- begin
- AppTopic := HiWord(Msg.LParam);
- if not LinkEstablished then
- begin
- HWndServer := Msg.WParam; { Save server's window handle }
- SetWindowText(Parent^.HWindow, linkedCaption);
- LinkEstablished := true;
- AppAtom := LoWord(Msg.LParam);
- if AppAtom <> 0 then
- GlobalDeleteAtom(AppAtom)
- end else
- begin
- DDEStatus := LoWord(Msg.LParam);
- if (DDEStatus and dde_Ack) = 0 then
- MessageBox(Parent^.HWindow, 'Request for data failed',
- msgCaption, mb_Ok)
- end;
- if AppTopic <> 0 then
- GlobalDeleteAtom(AppTopic)
- end;
-
- {- Respond to transmission of data from server }
- procedure TConversation.WMDDEData(var Msg: TMessage);
- var
- AppTopic: TAtom; { Global atom describing data topic }
- HData: THandle; { Handle to data's memory block }
- PData: PDDEData; { Pointer to data }
- begin
- HData := LoWord(Msg.LParam);
- AppTopic := HiWord(Msg.LParam);
- {- Inspect topic here if necessary }
- if AppTopic <> 0 then
- GlobalDeleteAtom(AppTopic);
- if HData <> 0 then
- begin
- PData := GlobalLock(HData);
- if PData <> nil then
- begin
- if PData^.CfFormat = cf_Text then
- SetText(PChar(@PData^.Value))
- else
- MessageBox(Parent^.HWindow, 'Data received is not text',
- msgCaption, mb_Ok);
- GlobalUnlock(HData)
- end;
- GlobalFree(HData) {- Assume client will dispose global data }
- end
- end;
-
- {- Respond to receipt of wm_DDE_Terminate message from server }
- procedure TConversation.WMDDETerminate(var Msg: TMessage);
- begin
- if LinkEstablished then
- begin
- LinkEstablished := false; { Cancel the conversation }
- SetWindowText(Parent^.HWindow, wndCaption);
- MessageBox(Parent^.HWindow, 'Conversation terminated by server',
- msgCaption, mb_Ok)
- end
- end;
-
- {- Attempt to link TConversation window to server }
- procedure TConversation.LinkToServer;
- var
- AppAtom, AppTopic: TAtom;
- begin
- if not LinkEstablished then
- begin
- AppAtom := GlobalAddAtom(serverName);
- AppTopic := GlobalAddAtom(serverTopic);
- SendMessage(Word(-1), wm_DDE_Initiate, HWindow,
- MakeLong(AppAtom, AppTopic));
- GlobalDeleteAtom(AppAtom);
- GlobalDeleteAtom(AppTopic)
- end
- end;
-
- {- Terminate link (if there is one) from server }
- procedure TConversation.UnlinkFromServer;
- begin
- if LinkEstablished then
- begin
- if IsWindow(HWndServer) then
- PostMessage(HWndServer, wm_DDE_Terminate, HWindow, 0)
- else
- MessageBox(Parent^.HWindow, 'Server lost', msgCaption, mb_Ok);
- SetWindowText(Parent^.HWindow, wndCaption);
- LinkEstablished := false
- end
- end;
-
- {- Request text data from server }
- procedure TConversation.RequestText;
- var
- AppItem: TAtom;
- begin
- if LinkEstablished then
- begin
- if IsWindow(HWndServer) then
- begin
- AppItem := GlobalAddAtom(serverTopic);
- if not PostMessage(HWndServer, wm_DDE_Request, HWindow,
- MakeLong(cf_Text, AppItem)) then
- GlobalDeleteAtom(AppItem)
- end else
- begin
- SetWindowText(Parent^.HWindow, wndCaption);
- MessageBox(Parent^.HWindow, 'Server lost', msgCaption, mb_Ok);
- LinkEstablished := false { Lost server }
- end
- end
- end;
-
- var
-
- ClientApp: ClientApplication;
-
- begin
- ClientApp.Init('ClientApp');
- ClientApp.Run;
- ClientApp.Done
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 5/27/1991
- ---------------------------------------------------------------}
-